home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
CUGUK
/
PROG_TOO
/
C013.ZIP
/
RM.C
< prev
next >
Wrap
Text File
|
1990-01-19
|
4KB
|
160 lines
/********************************************************************
* C Users Group (U.K) C Source Code Library File CUGLIB.013 *
* Inquiries to: M. Houston, 36 Whetstone Clo. Farquhar Rd. *
* Edgbaston, Birmingham B15 2QN ENGLAND *
********************************************************************
* File name: rm.c
* Program name:rm
* Source of file: Ron Wellstead
* Purpose: An MS-DOS copy of the UNIX utility of the same name.
* Changes: <who what when & why major changes have been made>
********************************************************************/
/*
*
* @(#) rm.c 1.2 87/07/27
*
* UNIX style rm utility for dos
*
* copyright (c) 1987 Ron Wellsted.
* This software is provided on the understanding that it is
* NOT to be used for commercial gain. It may be freely distributed
* in source or object form among amateur and hobby computer users ONLY!
*
* removes files and directories
* usage: rm [-rfi] files../directories
* switches: -r recursive [USE WITH EXTREME CAUTION!]
* will remove ALL sub-directories below
* given names.
* -i inquire before removing file/directory
* -f prevents error message on non-existant
* files, read-only files etc. (BUT WILL remove
* r/o files).
* written for Microsoft C, link with setargv.obj to expand wildcards
*/
#include <stdio.h>
#include <process.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
char *name,*swch,buffer[256]="@(#)rm.c VR 1.0.0 15 Jul 1987";
int fsw=0,rsw=0,isw=0;
struct stat data;
main(argc,argv)
int argc;
char *argv[];
{
int i,rm;
if (argc>=2) swch=argv[1];
if ((argc<2)||(setsw(argv[1])<0))
{
fprintf(stderr,"usage rm [-fri] file1 [.... fileN]\n");
exit(1);
}
name=argv[0];
if (fsw|rsw|isw) i=2;
else i=1;
for (;i<argc;i++)
{
if (stat(argv[i],&data))
{
fprintf(stderr,"rm: ");
perror(argv[i]);
if (rsw) continue;
else exit(1);
}
if (data.st_mode & S_IFDIR)
{
if (rsw==0) continue;
if (cleardir(argv[i]))
{
fprintf(stderr,"rm: cannot remove %s",argv[i]);
if (rsw) continue;
else exit(1);
}
}
else
{
if (!(data.st_mode & S_IWRITE))
{
rm=fsw;
if (!rm) rm=check(argv[i],"read only: ");
if (rm) chmod(argv[i],S_IWRITE);
}
if (isw) rm=check(argv[i],"\0");
if (!rm) continue;
if (unlink(argv[i]))
{
fprintf(stderr,"rm: ");
perror(argv[i]);
exit(1);
}
}
}
exit(0);
}
setsw(ptr)
char *ptr;
{
char ch;
if (*ptr!='-') return 0;
for (ptr++;*ptr!='\0';ptr++)
{
ch=tolower(*ptr);
if (ch=='f') fsw=1;
else if (ch=='r') rsw=1;
else if (ch=='i') isw=1;
else
{
fprintf(stderr,"rm: %c: illegal switch n",ch);
return -1;
}
}
return 1;
}
check(ptr,mode)
char *ptr,*mode;
{
char reply[20];
printf("%s: %sremove ? ",ptr,mode);
if (gets(reply)==NULL) return 0;
if (tolower(reply[0])=='y') return 1;
else return 0;
}
cleardir(dir)
char *dir;
{
char ch;
int retval;
strcpy(buffer,dir);
ch=buffer[strlen(buffer)-1];
if ((ch!='\\')&&(ch!='/'))
strcat(buffer,"\\");
strcat(buffer,"*.*");
retval=spawnl(P_WAIT,name,name,swch,buffer,NULL);
if (retval<0)
{
perror("rm: ");
exit(1);
}
if (retval==0)
{
if (rmdir(dir))
{
fprintf(stderr,"rm: ");
perror(dir);
if (rsw) retval=1;
else exit(1);
}
}
return retval;
}